//利用数组排序 减少遍历次数O(n*logN)
//当使用思路和代码框架都比较完整,依然时间效率低,尝试往数据结构的方向突破
#include<algorithm>
#include<vector>
using namespace std;
class Solution {
public:
    int findMinDifference(vector<string>& timePoints) {
        if(timePoints.size()>1440) return 0;
        vector<int>vt;
        vt.reserve(timePoints.size());
        for(auto&e:timePoints){
            vt.push_back(stoi(e)*60+stoi(e.substr(3)));
        }
        sort(vt.begin(),vt.end());
        int res=1440-vt.back()+vt.front();
        for(int i=0,j=1;res&&j<timePoints.size();i++,j++){
            res=min(res,vt[j]-vt[i]);
        }
        return res;
    }
};
//O(N^2)
#include <vector>
#include <unordered_map>
#include <cmath>
#include <climits>
using namespace std;
class Solution
{
public:
    int des(int m1, int m2)
    {
        int d = abs(m1 - m2);
        return min(d, 1440 - d);
    }
    int findMinDifference(vector<string> &timePoints)
    {
        if (timePoints.size() > 1440)
            return 0;
        unordered_map<int, int> u_map;
        for (int i = 0; i < timePoints.size(); i++)
        {
            string &e = timePoints[i];
            u_map[i] = stoi(e) * 60 + stoi(e.substr(3));
        }
        int res = INT_MAX;
        for (int i = 0; i < timePoints.size(); i++)
        {
            for (int j = 0; j < i; j++)
            {
                res = min(res, des(u_map[i], u_map[j]));
            }
        }
        return res;
    }
};